import i18next from "i18next";
import { initReactI18next } from "react-i18next";
import Backend from "i18next-http-backend";
import LanguageDetector from "i18next-browser-languagedetector";
import { getInitialNamespaces } from "remix-i18next/client";
import { i18nConfig } from "./config";

export async function getClientI18n() {
  const i18n = i18next.createInstance();

  await i18n
    .use(initReactI18next)
    .use(Backend)
    .use(LanguageDetector)
    .init({
      ...i18nConfig,
      ns: getInitialNamespaces(),
      backend: {
        loadPath: "/locales/{{lng}}/{{ns}}.json",
      },
      detection: {
        order: ["htmlTag", "querystring", "cookie"],
      },
      react: {
        useSuspense: false,
      },
    });

  return i18n;
}
